package endoscopic

import (
	"fmt"
	"rongyy/src/app"
	"rongyy/src/middlewares"
	"rongyy/src/models"
)

type PatientInfo struct {
	models.BaseModel    `xorm:"extends"`
	OperativeSite       string     `xorm:"operative_site" json:"operative_site"`
	Name                string     `xorm:"name" json:"name"`
	PatientId           string     `xorm:"patient_id" json:"patient_id"`
	IsDie               string     `xorm:"is_die" json:"is_die"`
	DieDesc             string     `xorm:"die_desc" json:"die_desc"`
	AdmissionNum        string     `xorm:"admission_num" json:"admission_num"`
	AllOrGig            string     `xorm:"all_or_big" json:"all_or_big"`
	WechatName          string     `xorm:"wechat_name" json:"wechat_name"`
	Mobile              string     `xorm:"mobile" json:"mobile"`
	OperativeTime       string     `xorm:"operative_time" json:"operative_time"`
	OperativeType       string     `xorm:"operative_type" json:"operative_type"`
	PathologyType       string     `xorm:"pathology_type" json:"pathology_type"`
	IsIhc               string     `xorm:"is_ihc" json:"is_ihc"`
	Differentiation     string     `xorm:"differentiation" json:"differentiation"`
	InfType             string     `xorm:"inf_type" json:"inf_type"`
	InfDepth            string     `xorm:"inf_depth" json:"inf_depth"`
	VasInfo             string     `xorm:"vas_info" json:"vas_info"`
	HorizontalMargin    string     `xorm:"horizontal_margin" json:"horizontal_margin"`
	VerticalMargin      string     `xorm:"vertical_margin" json:"vertical_margin"`
	AddCure             string     `xorm:"add_cure" json:"add_cure"`
	EsdComplication     string     `xorm:"esd_complication" json:"esd_complication"`
	SurgeryComplication string     `xorm:"surgery_complication" json:"surgery_complication"`
	OperativeStricture  string     `xorm:"operative_stricture" json:"operative_stricture"`
	StrictureCure       string     `xorm:"stricture_cure" json:"stricture_cure"`
	FollowUp            []FollowUp `xorm:"-" json:"follow_up"`
	RequestDate         string     `xorm:"-" json:"request_date"`
	MsgDate             string     `xorm:"msg_date" json:"msg_date"`
	MsgStatus           string     `xorm:"msg_status" json:"msg_status"`
}

func (pm *PatientInfo) TableName() string {
	return "endoscopic_patient_info"
}

func (pm *PatientInfo) Insert() error {
	_, err := app.DB().Insert(pm)
	if err != nil {
		middlewares.Logger.Println("ERROR - PatientInfo Insert failed ", err)
		return err
	}
	return nil
}

func (pm *PatientInfo) SelectByPatientId(patientId string) (*PatientInfo, error) {
	am := &PatientInfo{}
	has, err := app.DB().Where("patient_id = ? ", patientId).Limit(1).Get(am)
	if err != nil {
		return nil, err
	}
	if !has {
		return nil, nil
	}
	return am, nil
}

func (pm *PatientInfo) SelectById(id int64) (*PatientInfo, error) {
	am := &PatientInfo{}
	has, err := app.DB().Where("id = ? ", id).Limit(1).Get(am)
	if err != nil {
		return nil, err
	}
	if !has {
		return nil, nil
	}
	return am, nil
}

func (pm *PatientInfo) Update(req *PatientInfo) error {
	_, err := app.DB().ID(req.Id).AllCols().Update(req)
	if err != nil {
		middlewares.Logger.Println("ERROR - type Update failed ", err)
		return fmt.Errorf("更新失败")
	}
	return nil
}

func (pm *PatientInfo) SelectByList(limit int, start int, name string, patient_id string, operative_type string) ([]PatientInfo, int64, error) {
	data := make([]PatientInfo, 0)
	s := app.DB().Table("endoscopic_patient_info")
	if name != "" {
		s.Where("name like ?", "%"+name+"%")
	}
	if patient_id != "" {
		s.Where("patient_id like ?", "%"+patient_id+"%")
	}
	if operative_type != "" {
		s.Where("operative_type like ?", "%"+operative_type+"%")
	}
	total, err := s.Limit(limit, limit*start).Desc("created_at").FindAndCount(&data)
	if err != nil {
		return nil, 0, err
	}
	return data, total, nil
}

func (pm *PatientInfo) DeleteById(req *PatientInfo) error {
	data := &PatientInfo{}
	data.Id = req.Id
	_, err := app.DB().ID(req.Id).Delete(data)
	if err != nil {
		middlewares.Logger.Println("ERROR - type Update failed ", err)
		return fmt.Errorf("删除失败")
	}
	return nil
}
